package com.harmonk.firstscene.ui

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.DividerDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.harmonk.firstscene.R
import androidx.core.net.toUri
import com.harmonk.firstscene.constants.CommonConstants

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AboutScreen(
    onBackClick: () -> Unit
) {
    val context = LocalContext.current
    var versionName by remember { mutableStateOf("未知") }
    var versionCode by remember { mutableStateOf("未知") }

    LaunchedEffect(Unit) {
        try {
            val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
            versionName = packageInfo.versionName ?: "未知"
            versionCode = packageInfo.longVersionCode.toString()
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
    }

    Scaffold(
        topBar = {
            androidx.compose.material3.TopAppBar(
                title = { Text("关于") },
                navigationIcon = {
                    androidx.compose.material3.IconButton(onClick = onBackClick) {
                        androidx.compose.material3.Icon(
                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "返回"
                        )
                    }
                }
            )
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.SpaceEvenly
        ) {
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                // 应用图标
                Image(
                    painter = painterResource(id = R.drawable.start_icon),
                    contentDescription = "应用图标",
                    modifier = Modifier
                        .size(120.dp)
                        .padding(16.dp)
                )

                // 应用名称
                Text(
                    text = stringResource(id = R.string.app_name),
                    style = MaterialTheme.typography.headlineMedium,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(bottom = 24.dp)
                )
            }

            // 软件介绍
            Text(
                text = "现场速记是一款便捷记录和整理道路交通事故现场相关信息的工具应用。 \n" +
                        "主要功能： \n" +
                        "· 记录事故时间、道路、方向、位置、描述和损伤情况； \n" +
                        "· 记录车辆和人员相关信息； \n" +
                        "· 同时记录时间和经纬度信息的记事；\n" +
                        "· 生成并分享文字报告等。",
                style = MaterialTheme.typography.bodyMedium,
                modifier = Modifier.padding(bottom = 24.dp)
            )

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 12.dp)
            ) {
                // 版本号
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = "版本",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                    Text(
                        text = "$versionName ($versionCode)",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                }
                HorizontalDivider(Modifier, DividerDefaults.Thickness, DividerDefaults.color)

                // 隐私政策
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                        .clickable {
                            // 拉起浏览器跳转到隐私政策页面
                            val intent = Intent(Intent.ACTION_VIEW)
                            intent.data = CommonConstants.PRIVACY_POLICY_URL.toUri()
                            context.startActivity(intent)
                        },
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = "隐私政策",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                    Text(
                        text = ">",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                }
                HorizontalDivider(Modifier, DividerDefaults.Thickness, DividerDefaults.color)

                // 用户协议
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                        .clickable {
                            // 拉起浏览器跳转到用户协议页面
                            val intent = Intent(Intent.ACTION_VIEW)
                            intent.data = CommonConstants.USER_AGREEMENT_URL.toUri()
                            context.startActivity(intent)
                        },
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = "用户协议",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                    Text(
                        text = ">",
                        style = MaterialTheme.typography.bodyMedium,
                        modifier = Modifier
                    )
                }
                HorizontalDivider(Modifier, DividerDefaults.Thickness, DividerDefaults.color)

                // 联系作者
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = "联系作者",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                    Text(
                        text = "QQ: 232344787",
                        style = MaterialTheme.typography.bodyMedium,
                    )
                }
            }
        }
    }
}